package models;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;

import net.ufrog.common.utils.Strings;
import play.cache.Cache;
import utils.dict.Element;

/**
 * 自动回复模型
 * 
 * @author ultrafrog
 * @version 1.0, 2014-12-17
 * @since 1.0
 */
@Entity
@Table(name = "uwx_reply")
public class Reply extends Model {

	private static final String CACHE_MATCHING_PERFECT	= "play_weixin_cache_keyword_perfect_";
	private static final String CACHE_MATCHING_PARTIAL	= "play_weixin_cache_keyword_partial_";
	private static final String CACHE_MATCHING_DEFAULT	= "play_weixin_cache_keyword_default_";
	
	/** 名称 */
	@Column(name = "vc_name")
	public String name;
	
	/** 内容 */
	@Column(name = "vc_value")
	public String value;
	
	/** 类型 */
	@Column(name = "vc_type")
	public String type;
	
	/** 公众号 */
	@Column(name = "fk_weixin")
	public String weixin;
	
	/** 关键字 */
	@Transient
	public List<ReplyKeyword> keywords;
	
	/** 构造函数 */
	public Reply() {}
	
	/**
	 * 构造函数 
	 * 
	 * @param name
	 * @param value
	 * @param type
	 * @param weixin
	 */
	public Reply(String name, String value, String type, String weixin) {
		this();
		this.name = name;
		this.value = value;
		this.type = type;
		this.weixin = weixin;
	}
	
	/**
	 * 读取关键字
	 * 
	 * @return
	 */
	public List<ReplyKeyword> getKeywords() {
		if (keywords == null) {
			keywords = ReplyKeyword.find("reply = ? and type in (?, ?)", id, ReplyKeyword.Type.PERFECT_MATCHING, ReplyKeyword.Type.PARTIAL_MATCHING).fetch();
		}
		return keywords;
	}
	
	/**
	 * 读取所有完全匹配关键字
	 * 
	 * @param weixin
	 * @return
	 */
	public static Map<String, List<Reply>> getPerfectMatching(String weixin) {
		if (Cache.get(CACHE_MATCHING_PERFECT + weixin) == null) {
			generateKeywordMap(weixin);
		}
		return Cache.get(CACHE_MATCHING_PERFECT + weixin, Map.class);
	}
	
	/**
	 * 读取所有部分匹配关键字
	 * 
	 * @param weixin
	 * @return
	 */
	public static Map<String, List<Reply>> getPartialMatching(String weixin) {
		if (Cache.get(CACHE_MATCHING_PARTIAL + weixin) == null) {
			generateKeywordMap(weixin);
		}
		return Cache.get(CACHE_MATCHING_PARTIAL + weixin, Map.class);
	}
	
	/**
	 * 读取所有默认匹配关键字
	 * 
	 * @param weixin
	 * @return
	 */
	public static Map<String, List<Reply>> getDefaultMatching(String weixin) {
		if (Cache.get(CACHE_MATCHING_DEFAULT + weixin) == null) {
			generateKeywordMap(weixin);
		}
		return Cache.get(CACHE_MATCHING_DEFAULT + weixin, Map.class);
	}
	
	/**
	 * 清除缓存
	 * 
	 * @param weixin
	 */
	public static void clear(String weixin) {
		Cache.delete(CACHE_MATCHING_PERFECT + weixin);
		Cache.delete(CACHE_MATCHING_PARTIAL + weixin);
		Cache.delete(CACHE_MATCHING_DEFAULT + weixin);
	}
	
	/**
	 * 生成关键字映射
	 * 
	 * @param weixin
	 */
	protected static void generateKeywordMap(String weixin) {
		// 初始化
		Map<String, List<Reply>> perfectKw = new HashMap<String, List<Reply>>();
		Map<String, List<Reply>> partialKw = new HashMap<String, List<Reply>>();
		Map<String, List<Reply>> defaultKw = new HashMap<String, List<Reply>>();
		List<Reply> replys = Reply.find("byWeixin", weixin).fetch();
		
		// 遍历处理关键字
		for (Reply reply: replys) {
			// 初始化并查询关键字
			Map<String, List<Reply>> temp = null;
			List<ReplyKeyword> keywords = ReplyKeyword.find("byReply", reply.id).fetch();
			
			// 遍历判断
			for (ReplyKeyword rk: keywords) {
				// 判断当前类型
				if (Strings.equals(rk.type, ReplyKeyword.Type.PERFECT_MATCHING)) {
					temp = perfectKw;
				} else if (Strings.equals(rk.type, ReplyKeyword.Type.PARTIAL_MATCHING)) {
					temp = partialKw;
				} else if (Strings.equals(rk.type, ReplyKeyword.Type.DEFAULT_MATCHING)) {
					temp = defaultKw;
				}
				
				// 设置关键字映射
				if (!temp.containsKey(rk.keyword)) {
					temp.put(rk.keyword, new ArrayList<Reply>());
				}
				temp.get(rk.keyword).add(reply);
			}
		}
		
		// 缓存数据
		Cache.set(CACHE_MATCHING_PERFECT + weixin, perfectKw);
		Cache.set(CACHE_MATCHING_PARTIAL + weixin, partialKw);
		Cache.set(CACHE_MATCHING_DEFAULT + weixin, defaultKw);
	}
	
	/**
	 * 类型
	 * 
	 * @author ultrafrog
	 * @version 1.0, 2014-12-25
	 * @since 1.0
	 */
	public static final class Type {
		
		@Element("文本")
		public static final String TEXT		= "01";
		
		@Element("单图文消息")
		public static final String SINGLE	= "02";
		
		@Element("多图文消息")
		public static final String MULTI	= "03";
	}
}
